/*
 * 文件名：ConsumableTransactionImpl.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wuliquan
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.consumables.consumablesInfo.transaction.impl;

import org.springframework.beans.BeanUtils;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.dao.CommonDao;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.consumables.consumablesInfo.dao.ConsumablesInfoDao;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesInfo;
import com.wootide.cms.service.consumables.consumablesInfo.pojo.ConsumablesReqRecord;
import com.wootide.cms.service.consumables.consumablesInfo.transaction.ConsumablesInfoTransaction;

/**
 * 易耗品事务层接口实现
 * 〈功能详细描述〉
 * @author wuliquan
 * @version 2013-2-23
 * @see ConsumablesInfoTransactionImpl
 * @since
 */
public class ConsumablesInfoTransactionImpl implements ConsumablesInfoTransaction
{
    /**
     * 注入运行日志
     */
    private RunLog runLog;
    
    /**
     * 公共数据访问层实例
     */
    private CommonDao commonDao;
    
    /**
     * 易耗品数据访问层实例
     */
    private ConsumablesInfoDao consumablesInfoDao;
    
    public void addOneConsumablesInfo(ConsumablesInfo consumablesInfo)
    {
        // 打印方法入口参数
        runLog.debug("consumablesInfo=" + consumablesInfo);
        // 根据易耗品名检查唯一性
        Integer num = consumablesInfoDao.findNumByConsumablesInfoName(consumablesInfo.getConsumablesName());
        //易耗品记录已经存在
        if (num > 0)
        {
            throw new CMSRuntimeException("ConsumablesInfo Existed!",
                    ErrorCodes.Consumables.ConsumablesInfo.COMSUMABLESINFO_EXIST);
        }
        commonDao.addOneObject(consumablesInfo);
    }
    
    public ConsumablesInfo delOneConsumablesInfo(Integer innerIndex)
    {
        ConsumablesInfo oldConsumableInfo = consumablesInfoDao.findOneConsumablesInfoByConsumableId(innerIndex);
        //判断要删除的记录是否存在
        if (null == oldConsumableInfo)
        {
            throw new CMSRuntimeException("ConsumablesInfo not Exist!",
                    ErrorCodes.Consumables.ConsumablesInfo.COMSUMABLESINFO_EXIST);
        }
        
        commonDao.delOneObject(oldConsumableInfo);
        
        return oldConsumableInfo;
    }
    
    public ConsumablesInfo modOneConsumablesInfo(ConsumablesInfo consumablesInfo)
    {
        // 打印方法入口参数
        runLog.debug("consumablesInfo=" + consumablesInfo);
        ConsumablesInfo oldConsumableInfo = consumablesInfoDao.findOneConsumablesInfoByConsumableId(consumablesInfo.getConsumablesId());
        
        //判断要修改的记录是否存在
        if (null == oldConsumableInfo)
        {
            throw new CMSRuntimeException("ConsumablesInfo not Exist!",
                    ErrorCodes.Consumables.ConsumablesInfo.COMSUMABLESINFO_NOTEXIST);
        }
        
        //获取原始数据副本
        ConsumablesInfo oldConsumableCopy = new ConsumablesInfo();
        BeanUtils.copyProperties(oldConsumableInfo, oldConsumableCopy);
        
        //修改易耗品信息
        oldConsumableInfo.setConsumablesName(consumablesInfo.getConsumablesName());
        oldConsumableInfo.setConsumablesBrand(consumablesInfo.getConsumablesBrand());
        oldConsumableInfo.setConsumablesQuantity(consumablesInfo.getConsumablesQuantity());
        oldConsumableInfo.setAlarmQuantity(consumablesInfo.getAlarmQuantity());
        oldConsumableInfo.setConsumablesPrice(consumablesInfo.getConsumablesPrice());
        oldConsumableInfo.setConsumablesTypeInfo(consumablesInfo.getConsumablesTypeInfo());
        oldConsumableInfo.setPurchaserDate(consumablesInfo.getPurchaserDate());
        oldConsumableInfo.setPurchaser(consumablesInfo.getPurchaser());
        
        //便于日志记录
        return oldConsumableCopy;
    }
    
    public QueryResults queryForPages(Page page, QueryCondition queryConditon)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryConditon=" + queryConditon);
        QueryResults queryResults = commonDao.queryForPages(page, queryConditon);
        return queryResults;
    }
    
    public ConsumablesInfo viewOneConsumablesInfo(Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + innerIndex);
        ConsumablesInfo consumableInfo = consumablesInfoDao.findOneConsumablesInfoByConsumableId(innerIndex);
        return consumableInfo;
    }
    
    public ConsumablesInfo exportConsumalbesInfo(
            ConsumablesReqRecord consumablesReqRecord, Integer innerIndex)
    {
        // 打印方法入口参数
        runLog.debug("consumablesReqRecord=" + consumablesReqRecord);
        runLog.debug("innerIndex=" + innerIndex);
        
        //获取要出库的易耗品信息
        ConsumablesInfo oldConsumableInfo = consumablesInfoDao.findOneConsumablesInfoByConsumableId(innerIndex);
        
        //判断要修改的记录是否存在
        if (null == oldConsumableInfo)
        {
            throw new CMSRuntimeException("ConsumablesInfo not Exist!",
                    ErrorCodes.Consumables.ConsumablesInfo.COMSUMABLESINFO_NOTEXIST);
        }
        
        //获取原始数据副本
        ConsumablesInfo oldConsumablesInfoCopy = new ConsumablesInfo();
        BeanUtils.copyProperties(oldConsumableInfo, oldConsumablesInfoCopy);
        
        
        //减去易耗品出库数量
        oldConsumableInfo.setConsumablesQuantity(oldConsumableInfo.getConsumablesQuantity()-consumablesReqRecord.getQuantity());
        
        commonDao.addOneObject(consumablesReqRecord);
        
        //便于日志记录
        return oldConsumablesInfoCopy;
    }
    
    public void setCommonDao(CommonDao commonDao)
    {
        this.commonDao = commonDao;
    }
    
    public void setConsumablesInfoDao(ConsumablesInfoDao consumablesInfoDao)
    {
        this.consumablesInfoDao = consumablesInfoDao;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
    
}
